// const wx = require('weixin-js-sdk')
import wx from "weixin-js-sdk";
import {server} from "@/utils/env";
import request from "@/utils/request";
var wexinForbidShare = (query) => {
		let userId
		return new Promise(async (resolve, reject) => {
			await request({
				url: "/generator/user/front/getinfo",
				method: "get",
			}).then((response) => {
				userId = response.user.id;
				localStorage.setItem('user',response.user.id)
			});
			await request({
					url: "generator/user/front/sign",
					method: "post",
					data: {
						url:  location.href.split('#')[0]
					},
				})
				//getShare(data) //这里我写了一个公用的接口请求js，这里正常axios请求就可以，只要拿到数据都可以
				.then((res) => {
					localStorage.setItem("jsapi_ticket", res.jsapi_ticket);
					//拿到后端给的这些数据
					// let appId = res.appId;
					let timestamp = res.timestamp;
					let nonceStr = res.nocestr;
					let signature = res.signature;
					let shareUserId = userId;
					let config = {
						title: '搏一搏，单车变摩托' ,
						desc: "获得积分",
						link: res.url,
						imgUrl: "https://demoee.oss-cn-shanghai.aliyuncs.com/point/1637999742809.jpg",
						type: "",
						dataUrl: "",
						success: function () {
							request({
							  url: "/generator/user/front/share?way=14&shareuserId="+userId,
							  method: "get",
							}).then((response) => {});
						},
						cancel: function () {
							console.log("取消分享!");
						},
					};
					wx.config({
						debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						appId: server.appId, // 必填，公众号的唯一标识，填自己的！
						timestamp: timestamp, // 必填，生成签名的时间戳，刚才接口拿到的数据
						nonceStr: nonceStr, // 必填，生成签名的随机串
						signature: signature, // 必填，签名，见附录1
						jsApiList: [
							"updateTimelineShareData",
							"updateAppMessageShareData",
							"onMenuShareAppMessage",
						],
					});
	
					wx.ready(function () {
						console.log(' wx.config成功', res);
						//分享给朋友
						wx.onMenuShareAppMessage(config);
						// wx.updateAppMessageShareData(config);
					});
					wx.error(function (res) {
						console.log(' wx.config失败', res);
					});
				})
			resolve()
		})
	}
export default wexinForbidShare

